<template>
  <div class="base-withdraw">
    <van-empty v-if="isBuilding !== 0" description="当前无住房" />
    <div
      v-else
      style="height: 100%;"
    >
      <van-field
        readonly
        clickable
        :value="withdrawTime"
        label="退房时间"
        placeholder="选择日期"
        @click="showCalendar = true"
      />
      <van-calendar v-model="showCalendar" @confirm="onConfirmTime" get-container="#app" />
      <van-field
        type="textarea"
        v-model="withdrawContent"
        placeholder="退房理由"
        style="margin-top: 20px;"
      />
      请对本次住房做出评价
      <van-rate
        void-icon="star"
        style="margin-left: 10px;"
        v-model="rate"
      />
      <van-button
        round block type="info"
        style="margin-top: 20px;"
        @click="onSubmit"
      >{{ buttonMsg }}</van-button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'WithDrawBuilding',
  data() {
    return {
      isBuilding: 0,
      withdrawTime: '',
      withdrawContent: '',
      rate: 5,
      showCalendar: false,
      buttonMsg:"提交"
    };
  },
  mounted() {
    this.HttpManager.getBuilddingByUid(sessionStorage.getItem("userid")).then(res => {
      this.$toast.clear(true);
      console.log(res)
      if(res.code === 12006){
        this.isBuilding = 1;
      }
      else if (res.code === 21009) {
        this.$notify({ type: "danger", message: res.msg});
        this.isBuilding = 1;
      }
      else if (res.code === 21010) {
        this.$notify({ type: "warning", message: res.msg});
        this.isBuilding = 1;
      }
      else if(res.code === 22009){
        this.$notify({ type: "warning", message: res.msg});
        this.buttonMsg = "修改";
        this.rate = res.data.rate;
        this.withdrawContent = res.data.reason;
        this.withdrawTime = res.data.time;
      }
      else if(res.code === 22010){
        this.$notify({ type: "danger", message: res.msg});
        this.buttonMsg = "修改";
        this.rate = res.data.rate;
        this.withdrawContent = res.data.reason;
        this.withdrawTime = res.data.time;
      }
    });
  },
  methods: {
    onSubmit () {
      if(this.buttonMsg === "提交"){
        this.HttpManager.buildingwithdraw_insert({
          "uid":sessionStorage.getItem("userid"),
          "reason":this.withdrawContent,
          "rate":this.rate
        }).then(res =>{
          console.log(res);
          if(res.code === 0)
            this.$notify({ type: "success", message: "申请成功"});
          else
            this.$notify({ type: "danger", message: "申请失败"});
        })
      }
      else if(this.buttonMsg === "修改"){
        this.HttpManager.buildingwithdraw_update({
          "uid":sessionStorage.getItem("userid"),
          "reason":this.withdrawContent,
          "rate":this.rate
        }).then(res =>{
          if(res.code === 0)
            this.$notify({ type: "success", message: "修改成功"});
          else
            this.$notify({ type: "danger", message: "修改失败"});
        })
      }
    },
    onConfirmTime (date) {
      this.withdrawTime = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} 00:00:00`;
      this.showCalendar = false;
    },
  },
}
</script>

<style lang="less" scoped>
  .base-withdraw {
    height: 100%;
    overflow: auto;
    padding: 20px;

    /deep/.van-cell {
      padding: 0;
    }
  }
</style>
